// void, Obj me, Obj u

Unit this;
Building bld;
point pt;

Sleep(rand(100)+30);
this = me.AsUnit();
bld = u.AsBuilding;
if (!.IsValid()) return;
if (!bld.IsValid()) return;

pt = u.AsBuilding.pos;

if (.IsValid) {// leave parry mode at new command
	.SetParryMode(false);
	
	if(.hero.IsValid){
		if(.hero.HasItem("irage_battle")){
			Sleep(rand(200)+30);
			.AddCommand(true, "engage");
			return;
		} else if(.hero.HasItem("irage_battle_stand")){
			Sleep(rand(200)+30);
			.AddCommand(true, "stand_position");
			return;
		}
	}
	
	if(.item_count > 0){
		Item item;
		
		item = .FindItem("Terrain bonus I");
		if(item.IsValid)
			.RemoveItemsOfType("Terrain bonus I");
		else {
			item = .FindItem("Terrain bonus II");
			if(item.IsValid)
				.RemoveItemsOfType("Terrain bonus II");
			else {
				item = .FindItem("Terrain bonus III");
				if(item.IsValid)
					.RemoveItemsOfType("Terrain bonus III");
				else {
					item = .FindItem("Terrain bonus IV");
					if(item.IsValid)
						.RemoveItemsOfType("Terrain bonus IV");
				}
			}
		}
	}
}

if(bld.IsValid){

	if(bld.settlement.IsIndependent()){
		Sleep(rand(100)+50);
		.AddCommand(false, "advance", pt);
		return;
	}
}